package com.telnetnz.ticketdesk.service;

import java.util.ArrayList;
import java.util.List;

import net.sf.ehcache.Element;

import org.apache.log4j.Logger;

import com.telnetnz.ticketdesk.bean.ProjectToUser;
import com.telnetnz.ticketdesk.common.config.SpringContext;
import com.telnetnz.ticketdesk.dao.entity.ProjectToUserEntity;
import com.telnetnz.ticketdesk.dao.wrapper.ProjectToUserWrap;

public class ProjectToUserService
{
	public static final String[] ROLES = ProjectToUserWrap.ROLES;
	public static final int ROLE_MEMBER = 0;
	public static final int ROLE_MANAGER = 1;

	private static Logger logger = Logger.getLogger(ProjectToUserService.class);
	private static String CACHE_KEY = "id_";
	private static String CACHE_KEY_U = "u_";
	private static String CACHE_KEY_P = "p_";

	/**
	 * Important: This is the only entry to get the project bean, including the methods that return instances list
	 * 
	 * @param uid
	 * @return
	 */
	public static ProjectToUser getProjectToUser(int projectId, long userId)
	{
		Element elm = getWrap().getCache().get(CACHE_KEY + projectId + "_" + userId);
		ProjectToUser bean = null;
		if (elm == null)
		{
			ProjectToUserEntity entity = getWrap().selectByProjectIdAndUserId(projectId, userId);
			if (entity != null)
			{
				bean = ProjectToUser.adapt(entity);
				getWrap().getCache().put(new Element(CACHE_KEY + projectId + "_" + userId, bean));
			}
		}
		else
		{
			bean = (ProjectToUser) elm.getValue();
		}
		return bean;
	}

	public static void add(int projectId, long userId, int role)
	{
		getWrap().insert(projectId, userId, role);
		getWrap().getCache().remove(CACHE_KEY + projectId + "_" + userId);
		getWrap().getCache().remove(CACHE_KEY_P + projectId);
		getWrap().getCache().remove(CACHE_KEY_U + userId);
	}

	public static void delete(int projectId, long userId)
	{
		getWrap().deleteProjectToUser(projectId, userId);
		getWrap().getCache().remove(CACHE_KEY + projectId + "_" + userId);
		getWrap().getCache().remove(CACHE_KEY_P + projectId);
		getWrap().getCache().remove(CACHE_KEY_U + userId);
	}

	public static List<ProjectToUser> getByProjectId(int projectId)
	{
		Element elm = getWrap().getCache().get(CACHE_KEY_P + projectId);
		List<ProjectToUser> bean;
		if (elm == null)
		{
			List<ProjectToUserEntity> entities = getWrap().selectByProjectId(projectId);
			bean = adaptList(entities);
			getWrap().getCache().put(new Element(CACHE_KEY_P + projectId, bean));
		}
		else
		{
			bean = (List<ProjectToUser>) elm.getValue();
		}
		return bean;
	}
	public static void deleteByProjectId(int projectId)
	{
		getWrap().deleteByProjectId(projectId);
		getWrap().getCache().remove(CACHE_KEY_P + projectId);
	}

	public static List<ProjectToUser> getByUserId(long userId)
	{
		Element elm = getWrap().getCache().get(CACHE_KEY_U + userId);
		List<ProjectToUser> bean;
		if (elm == null)
		{
			List<ProjectToUserEntity> entities = getWrap().selectByUserId(userId);
			bean = adaptList(entities);
			getWrap().getCache().put(new Element(CACHE_KEY_U + userId, bean));
		}
		else
		{
			bean = (List<ProjectToUser>) elm.getValue();
		}
		return bean;
	}
	public static void deleteByUserId(long userId)
	{
		getWrap().deleteByUserId(userId);
		getWrap().getCache().remove(CACHE_KEY_U + userId);
	}

	private static List<ProjectToUser> adaptList(List<ProjectToUserEntity> entities)
	{
		List<ProjectToUser> beans = new ArrayList<ProjectToUser>();
		if (entities != null && entities.size() > 0)
		{
			for (ProjectToUserEntity entity : entities)
			{
				ProjectToUser bean = getProjectToUser(entity.getProjectId(), entity.getUserId());
				if (bean != null)
					beans.add(bean);
			}
		}
		return beans;
	}

	private static ProjectToUserWrap getWrap()
	{
		return SpringContext.getProjectToUserWrap();
	}

	public static void main(String[] args)
	{
		
	}
}
